<template>
  <a-modal
    :title="title"
    :width="450"
    :visible="visible"
    :closable="false"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
  >
    <template slot="footer">
      <a-button @click="close">关闭</a-button>
      <a-button @click="handleOk" type="primary">确认</a-button>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules" labelAlign="left">
        <a-form-model-item
          label="团队名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="name"
          help="格式：xxx医生工作室"
        >
          <a-input placeholder="请填写团队名称" :max-length="10" v-model="model.name" />
        </a-form-model-item>
        <a-form-model-item label="团队特色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
          <a-textarea placeholder="请填写团队特色" :max-length="100" :rows="5" v-model="model.description" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { postAction } from '@/api/manage'

export default {
  name: 'CreateTeamModal',
  data() {
    return {
      title: '操作',
      visible: false,
      confirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      model: {},
      validatorRules: {
        name: [{ required: true, message: '请填写团队名称!' }],
        description: [{ required: true, message: '请填写团队特色!' }]
      }
    }
  },
  methods: {
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth
      if (screenWidth < 500) {
        this.drawerWidth = screenWidth
      } else {
        this.drawerWidth = 700
      }
    },
    //窗口最大化切换
    toggleScreen() {
      if (this.modaltoggleFlag) {
        this.modalWidth = window.innerWidth
      } else {
        this.modalWidth = 800
      }
      this.modaltoggleFlag = !this.modaltoggleFlag
    },
    add(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.visible = false
      this.model = {}
      this.$refs.form.resetFields()
    },
    handleOk() {
      const _this = this
      // 触发表单验证
      _this.$refs.form.validate(valid => {
        // 验证表单没错误
        if (valid) {
          let queryData = JSON.parse(JSON.stringify(this.model))
          postAction('201/03/2010306', queryData)
            .then(res => {
              if (res.success) {
                _this.$message.success('操作成功')
                _this.close()
                this.$emit('ok')
              } else {
                _this.$message.fali(res.message)
              }
            })
            .finally(_ => {
              // _this.$message.success('操作成功')
              _this.$emit('ok')
            })
        } else {
          console.log('error')
        }
      })
    }
  }
}
</script>

<style scoped>
.valid-error .ant-select-selection__placeholder {
  color: #f5222d;
}
</style>

